/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import SQL.SQL;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import weibo4j.Weibo;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class cInfluence {

    static double delta = 0.0001;

    public static void main(String[] args) throws FileNotFoundException, IOException, WeiboException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Auth.Auth4Code.prepareWeibo();
        DataPreparation data = new DataPreparation();
        relationScore.relationScore(data);
        //savetoSQL(data);
    }

    public static void cInfluence(DataPreparation data) {
        Map<String, Integer> sortedLeaderRank = sortLeaderRank(data);
        List<String> fIDs = data.data.mFIDs;
        fIDs.remove("0");
        fIDs.remove(Config.uid);
        int N = fIDs.size();
        double jaccardCount = 0;
        for (String user : fIDs) {
            double jaccard = delta;
            if (data.jaccard.containsKey(user)) {
                jaccard = data.jaccard.get(user);
                if (jaccard == 0) {
                    jaccard = delta;
                }
            }
            jaccardCount += jaccard;
        }
        for (String user : fIDs) {
            double jaccard = delta;
            if (data.jaccard.containsKey(user)) {
                jaccard = data.jaccard.get(user);
                if (jaccard == 0) {
                    jaccard = delta;
                }
                jaccard /= (jaccardCount);
            }
            double leaderrank = (double) (sortedLeaderRank.get(user)) / N;

            data.influence.put(user, jaccard * leaderrank);

        }
    }

    public static Map<String, Integer> sortLeaderRank(DataPreparation data) {
        Map<String, Integer> sortedLeaderRank = new HashMap<String, Integer>();
        List<String> leaderRankStringList = new ArrayList<String>();
        List<String> fIDs = data.data.mFIDs;
        fIDs.remove("0");
        fIDs.remove(Config.uid);
        for (String s : fIDs) {
            String leaderrank = data.S.get(s).toString();
            leaderRankStringList.add(leaderrank + " " + s);
        }
        Collections.sort(leaderRankStringList);
        int i = 0;
        for (String s : leaderRankStringList) {
            String strings[] = s.split(" ");
            if (!strings[1].equals("0") && !strings[1].equals(Config.uid)) {
                i += 1;
                sortedLeaderRank.put(strings[1], i);
            }
        }
        return sortedLeaderRank;
    }

    private static void savetoSQL(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Set keys = data.influence.keySet();
        int count = keys.size();
        List<String> sortInfluence = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            Double value = data.influence.get(key);
            String keyValue = value.toString() + "|" + key;
            sortInfluence.add(keyValue);
        }
        Collections.sort(sortInfluence);
        String text = "";
        for (int i = 0; i < count; i++) {
            String keyValue = sortInfluence.get(count - i - 1);
            String s[] = keyValue.split("|");
            text += s[1] + "|";
        }
        String uid = Config.uid;
        String table = "wb_demo_user";
        String columns[] = {"influence"};
        String values[] = {text};
        SQL.SQL(SQL.updateValue(columns, values, table, "uid", uid));
    }
}
